/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.Panel;
import com.jb.web.bean.Label;
import com.jb.web.bean.TabbedPanel;
import com.jb.web.context.WebContext;

import java.util.Iterator;

import com.jb.faces.component.UIComponent;

public class TabbedPanelRenderer extends RegionRenderer {
    public StringBuffer renderChildren(WebContext ctx, UIComponent item) {
        TabbedPanel sect = (TabbedPanel)item;
        if ("top".equals(sect.getTabPosition()) || 
            "bottom".equals(sect.getTabPosition())) {
            return renderTB(ctx, sect);
        } else {
            return renderLR(ctx, sect);
        }
    }

    private StringBuffer renderLR(WebContext ctx, TabbedPanel sect) {
        StringBuffer html = new StringBuffer();
        if ((!sect.isHideTabs() || !ctx.isRunTime()) && 
            "left".equals(sect.getTabPosition())) {
            html.append(renderTabs(ctx, sect));
        }
        Panel subSect = sect.getSelected();
        if (subSect != null) {
            html.append(subSect.render(ctx));
        }
        if ((!sect.isHideTabs() || !ctx.isRunTime()) && 
            "right".equals(sect.getTabPosition())) {
            html.append(renderTabs(ctx, sect));
        }
        return html;
    }

    private StringBuffer renderTB(WebContext ctx, TabbedPanel sect) {
        StringBuffer html = new StringBuffer();
        if ((!sect.isHideTabs() || !ctx.isRunTime()) && 
            "top".equals(sect.getTabPosition()))
            html.append(renderTabs(ctx, sect));

        Panel subSect = sect.getSelected();

        if (subSect != null) {
            html.append(subSect.render(ctx));
        }

        if ((!sect.isHideTabs() || !ctx.isRunTime()) && 
            "bottom".equals(sect.getTabPosition()))
            html.append(renderTabs(ctx, sect));
        return html;
    }

    private String renderTabs(WebContext ctx, TabbedPanel sect) {
        StringBuffer html = new StringBuffer("<div class=\"jb_tab_strip_div\"");
        html.append(addTagProperty("id", sect.getClientId() + "strip", null));
        html.append("><ul class=\"jb_tab_strip jb_tab_strip_top\" >");
        Iterator iter = sect.getChildPanels().iterator();
        while (iter.hasNext()) {
            Panel aSect = (Panel)iter.next();

            if (aSect != null) {
                Label aTitle = aSect.getHeader();

                if (!aTitle.isVisible(ctx)) {
                    continue;
                }

                String label = evaluate(ctx, aTitle, aTitle.getValue());

                html.append("<li ");
                html.append("class=\"\">");
                html.append("<a ");
                if(sect.isSelected(aSect))
                {
                    html.append(" onclick=");
                    html.append("\"JB._e('");
                    html.append(ctx.getPageName() + ".xsp");
                    html.append("','").append(aSect.getClientId()).append("','");
                    html.append("com.jb.web.bean.event.OnSelectTab");
                    html.append("','");
                    html.append(sect.getClientId());
                    html.append("');\"");
                }else
                {
                    html.append(" onclick='return false'");
                }
                
                html.append(" href=\"#\" class=\"jb_tab_right\">");
                html.append("<em class=\"jb_tab_left\">");
                html.append("<span class=\"jb_tab_strip_inner\" style=\"width: 90px;\">");
                html.append("<span class=\"jb_tab_strip_text tabs\">");
                html.append(label);
                html.append("</span></em></a></li>");
            }
            
        }
        html.append("</ul></div>");
        return html.toString();
    }
}
